<?php
  
  $constants = get_defined_constants();
  $href = $_GET['path'];
  Akrudo::raise_info(" Request: {$href}");
  $_GET['path'] = preg_replace('/\/$/', '', $_GET['path']);
  $_GET = explode('/', $_GET['path']);
  
  //$_GET = array_map('strtolower', $_GET);
  //if .httaccess passed dynamic address with filename UNSET 'public' element
  if ($_GET[0] == 'public') {
    array_shift($_GET);
  }
  
  //$controllers = $Nails->get_list_of_normal_controllers();
  //$controllers = array_map('strtolower', $controllers);
  //$modules = $Nails->get_list_of_extended_controllers();
  
  
  $param_names[0] = 'module';
  $param_names[1] = 'action';
  $param_names[2] = 'parameter';
  
  
  //pc($routes);
  //$routes = array_reverse($routes);
  //pc($routes);
  
  foreach ($this->Routes as $route) {
    $temp_tab = array();
    $match = true;
    $route_parts = explode('/', preg_replace('/\/$/' , '', $route[0]));
    if (count($route_parts) != count($_GET)) {
        $match = false;
        continue;
    }
    //Akrudo::raise_debug("Routing: Processing: {$route[0]}");
    //Akrudo::raise_debug("Routing: Number of elements matches for {$route[0]}");
    foreach ($route_parts as $key => $route_part) {
      switch ($route_part) {
        default:
          if (substr($route_part, 0, 1) == '$') {
            if (isset($route[2][$route_part]) && !preg_match('/' . $route[2][$route_part] . '/', $_GET[$key])) {
              $match = false;
            } else {
              $temp_tab[substr($route_part, 1)] = $_GET[$key];
            }
          } elseif ($route_part != $_GET[$key]) {
            $match = false;
          }
          break;
      }
      if (!$match) {
        break;
      }
    }
    if (isset($route[1])) {
      $predefined = $route[1];
      foreach ($predefined as $key => $val) {
        if (substr($val, 0, 1) == '|') {
          $temp_tab[$key] .= substr($val, 1);
        } elseif (substr($val, strlen($val) - 1, 1) == '|') {
          $temp_tab[$key] = substr($val, 0, strlen($val) - 1) . $temp_tab[$key];
        } else {
          $temp_tab[$key] = $val;
        }
      }
    }
    if ($match)
      break;
  }


  if ($match) {
    Akrudo::raise_info(" Routed:  {$route[0]}");
    define('USED_ROUTE', serialize($route));
  } else {
    Akrudo::raise_err("No route match found for: " . $href);
  }

  //pc($temp_tab);
  foreach ($temp_tab as $var_name => $value) {
    define($var_name, $value);
    //$Nails->view->assign($var_name, $value);
    $_REQUEST[$var_name] = $value;
  }
  if (defined('mod_suffix')) {
    define('mod_suffix_local', mod_suffix . S);
    define('mod_suffix_http', mod_suffix . '/');
  } else {
    define('mod_suffix', '');
    define('mod_suffix_local', '');
    define('mod_suffix_http', '');
  }
  
  $this->_request->controller = controller;
  $this->_request->action = action;
  /*

  	p(module);

  	p(action);

  	p(parameter);

  	p(parameter1);

  	p(parameter2);

  	p(parameter3);

   */
  $_REQUEST = array_merge($_REQUEST, $temp_tab);
  # $_REQUEST mirroring to $Params
  $this->request =& $_REQUEST;
  
?>